package com.jp.knowledge.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.google.gson.JsonObject;
import com.jp.knowledge.R;
import com.jp.knowledge.adapter.FragMentAdapter;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.ProductDetailFragment;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProductPlatform;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.util.TabLayoutUtil;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jiayen     2017-06-12 16:23:01
 * 产品详情
 */
public class ProductDetailActivity extends SlidingActivity {

    private FragMentAdapter adapter;

    private List<Fragment> fragments;

    private String id;
    /**
     * viewpage
     */
    @ViewInject(R.id.view_page)
    protected ViewPager viewPager;

    /**
     * tab
     **/
    @ViewInject(R.id.tab_view)
    protected TabView tabView;



    @Override
    protected int getResourceId() {
        return R.layout.activity_product_detail;
    }
    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");

        topName.setText("产品版本");
        rightIcon.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.guanzhufanhui);
        leftIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        getData();
    }

    /**
     * 获取数据
     */
    private  void getData(){
        JsonObject param = new JsonObject();
        param.addProperty("id",id);
        HttpHelper.getInstance(mContext).productPlatformList(param, 1, new ResultSubscriber.OnResultListener() {
            @Override
            public void onStart(int requestType) {

            }

            @Override
            public void onCompleted(int requestType) {
                L.i("ProductDetailActivity_getData","======onCompleted=========");
            }

            @Override
            public void onError(int requestType) {

            }

            @Override
            public void onNext(IModel data, int requestType) {
                L.i("ProductDetailActivity_getData","======onNext=========");
                List<ProductPlatform>  list = data.getList(ProductPlatform.class);
                setData(list);
            }
        });
    }

    /***
     * 设置数据
     * @param list
     */
    private void setData(List<ProductPlatform> list) {
        int size =list.size();
        fragments = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            fragments.add(new ProductDetailFragment().newInstance(id,list.get(i).getPlatformId()));
        }
        settabNames(list);
        adapter = new FragMentAdapter(getSupportFragmentManager(),fragments);
        viewPager.setAdapter(adapter);
        tabView.setupWithViewPager(viewPager);
        if(fragments.size() <= 1)
            tabView.setVisibility(View.GONE);
    }

    private void settabNames(List<ProductPlatform> list) {
        int size =list.size();
        List<String> tabNames = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            tabNames.add(list.get(i).getPlatformName());
        }
        tabView.setTabItems(tabNames);
    }
}
